﻿namespace fpsrpg
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;

    /// <summary>
    /// Units are Players and Npcs and other entities that act like them. They provide an abstraction
    /// so that the rendering engine can render a player or an npc.
    /// </summary>
    public partial class Unit : Sprite
    {
        #region Fields

        protected int health;
        protected uint MaxHealth;
        protected State mCurrentState = State.Walking;
        protected Vector2 mDirection = Vector2.Zero;
        protected Vector2 mSpeed = Vector2.Zero;

        uint level = 1;

        //protected State mCurrentState = State.Walking;
        Color[] _dataCache;

        #endregion Fields

        #region Constructors

        public Unit()
            : base()
        {
            MaxHealth = 100;
            Health = 100;
        }

        #endregion Constructors

        #region Enumerations

        protected enum State
        {
            Walking
        }

        #endregion Enumerations

        #region Properties

        public int Health
        {
            get { return health; }
              set
              {
              if (value < 1)
              {
                  health = 0;
                  return;
              }
              else
              {
                  health = value;
              }
              }
        }

        public bool IsDead
        {
            get { return health < 1; }
            set
            {
                if (value)
                {
                    Died();
                }
                else if (health < 1)
                {
                    Revive();
                }
            }
        }

        public uint Level
        {
            get { return level; }
              set { level = value; }
        }

        public virtual string Name
        {
            get { return GetType().Name; }
              set { }
        }

        public Color[] TextureData
        {
            get
              {
              if (_dataCache == null)
              {
                  _dataCache = new Color[Size.Width * Size.Height];

                  mSpriteTexture.GetData<Color>(_dataCache);
              }

              return _dataCache;
              }
              private set {}
        }

        #endregion Properties

        #region Methods

        public virtual void Died(Unit killer)
        {
            if(killer != null)
               killer.KilledOther(this);
        }

        public void Died()
        {
            Died(null);
        }

        public virtual void KilledOther(Unit victim)
        {
        }

        public virtual void Revive()
        {
            health = (int)MaxHealth;
        }

        public virtual void TookBullet(int dmg,Unit shooter)
        {
            Health -= dmg;
            if (IsDead)
            {
            Died(shooter);
            }
        }

        public override string ToString()
        {
            return Name;
        }

        #endregion Methods
    }
}